using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEngine.UI;

public class SkillEditorPanel : MonoBehaviour
{
    public ScrollRect scrollRect;
    public float TotalTime { get; set; }

    public GameObject TrackTrm;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void ShowBuff(SkillObject skill)
    {
        Debug.Log("UpdateSkill");
#if UNITY_EDITOR
        var self = this;
        DestroyChildrent(scrollRect.content);

        self.TotalTime = skill.TotalTime;

        foreach(var clip in skill.SkillClips)
        {
            switch (clip.ExecuteClipType)
            {
                case ExecuteClipType.CollisionExecute :

                    break;

                case ExecuteClipType.ActionEvent:

                    break;

                case ExecuteClipType.Animation:

                    break;

                case ExecuteClipType.ParticleEffect:

                    break;
                
                default:
                    break;
            }
        }
#endif
    }

    void DestroyChildrent(RectTransform parent)
    {
        for (int i = 0; i < parent.childCount; i++)
        {
            Destroy(parent.GetChild(i).gameObject);
        }
    }

    void LoadShowSkillCollisionClip(SkillClipData clip)
    {
        // var self = this;
        // var animTrack = Instantiate(self.TrackTrm);
        // animTrack.transform.SetParent(self.scrollRect.content);
        // clip.TotalTime = self.TotalTime;

        var animTrack = Instantiate(TrackTrm);
        animTrack.transform.SetParent(scrollRect.content);
        animTrack.GetComponentInChildren<Text>().text = clip.ExecuteClipType.ToString();

        clip.TotalTime = TotalTime;

        var trackClip = animTrack.GetComponent<TrackItem>();
        trackClip.SetClipType(clip);
    }

    void LoadShowSkillActionEventClip(SkillClipData clip)
    {
        var startTime = clip.StartTime;

        var actionTrack = Instantiate(TrackTrm);
        actionTrack.transform.SetParent(scrollRect.content);
        if(clip.ActionEventData != null)
        {
            actionTrack.GetComponent<Text>().text = "";
        }

        var trackClip = actionTrack.GetComponent<TrackItem>();

        trackClip.SliderLeft.value = startTime / TotalTime;
        trackClip.SliderRight.value = startTime / TotalTime + 0.01f;
        clip.TotalTime = TotalTime;
        trackClip.DisableSlider();
        trackClip.SetClipType(clip);
    }

    void LoadShowSkillAnimationClip(SkillClipData clip)
    {
        var anim = "anim";
        if(string.IsNullOrEmpty(anim))
        {
            return;
        }

        if(clip.AnimationData.AnimationAsset != null)
        {
            anim = clip.AnimationData.AnimationAsset.name;
        }

        var animTrack = Instantiate(TrackTrm);
        animTrack.transform.SetParent(scrollRect.content);
        animTrack.GetComponent<Text>().text = $"animation clip : {anim}";

        var trackClip = animTrack.GetComponent<TrackItem>();
        trackClip.SetClipType(clip);
    }

    void LoadShowSkillParticleEffectClip(SkillClipData clip)
    {
        var name = "";
        if(clip.ParticleEffectData.ParticleAsset != null)
        {
            name = clip.ParticleEffectData.ParticleAsset.name;
        }

        var animTrack = Instantiate(TrackTrm);
        animTrack.transform.SetParent(scrollRect.content);
        animTrack.GetComponent<Text>().text = name;

        var trackClip = animTrack.GetComponent<TrackItem>();
        trackClip.SetClipType(clip);
    }

    void LoadShowSkillSound(SkillClipData clip)
    {
        var sound = "";
        if(string.IsNullOrEmpty(sound))
        {
            return;
        }

        var audio = AssetDatabase.LoadAssetAtPath<AudioClip>(sound);
        if(audio == null)
        {
            return;
        }
        var audioTrack = Instantiate(TrackTrm);
        audioTrack.transform.SetParent(scrollRect.content);
        audioTrack.GetComponent<Text>().text = $"sound : {audio.name}";

        var trackClipData = new SkillClipData();
        trackClipData.TotalTime = TotalTime;
        trackClipData.ExecuteClipType = ExecuteClipType.Audio;
        trackClipData.AudioData = new AudioData();
        trackClipData.StartTime = 0;
        trackClipData.EndTime = audio.length;

        var trackClip = audioTrack.GetComponent<TrackItem>();
        trackClip.SetClipType(trackClipData);
        trackClip.SetDragEvent();
        trackClip.SliderRight.value = audio.length;
        trackClip.ClipTypeBar.color = trackClip.AudioClipBarColor;
    }

    public void SaveSkill()
    {

    }

    public void AddSkillClip()
    {

    }

    public void PlaySkill()
    {

    }

    public void StopSkill()
    {

    }
}
